@extends('layouts.adminlayout')

@section('main')
<p>{{ link_to_route('news.create', 'Thêm bài viết',null,array('class'=>'btn btn-info')) }}</p>

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">Bài Viết <small>Thông báo, tin tức, chuyên sinh</small></h3>
                </div><!-- /.box-header -->
                <div class="box-body table-responsive">

                    <table class="table table-hover table-bordered" id='dataTable'>
                        <thead>
                            <tr>
                                <th>Tiêu đề</th>
                                <th>Tóm tắt</th>
                                <th>Thứ tự</th>
                                <th>Chuyên mục</th>
                                <th></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <th>Tiêu đề</th>
                                <th>Tóm tắt</th>
                                <th>Thứ tự</th>
                                <th>Chuyên mục</th>
                                <th></th>
                                <th></th>
                            </tr>
                        </tfoot>
                        <tbody>
                            @foreach ($bai_viets as $bai_viet)
                            <tr>
                                <td>{{{ $bai_viet->tieu_de }}}</td>
                                <td>{{{strip_tags($bai_viet->tom_tat)}}}</td>
                                <td>
                                     <?php 
                                        $bai_viet_model = new Bai_viet();
                                        $thu_tu_lon_nhat = $bai_viet_model->get_max_thutu();
                                        $thu_tu_nho_nhat = $bai_viet_model->get_min_thutu();
                                    if($bai_viet->thu_tu_bai_viet == $thu_tu_lon_nhat){
                                        ?>
                                      <a type="button" class="btn btn-default" onclick="downconvert({{{$bai_viet->id}}})"><i class='fa fa-level-down'></i></a> 
                                      <?php
                                    }elseif($bai_viet->thu_tu_bai_viet == $thu_tu_nho_nhat){
                                        ?>
                                 
                                    <a type="button" class="btn btn-default" onclick="upconvert({{{$bai_viet->id}}})"><i class='fa fa-level-up'></i></a>
                                    <?php
                                    }else{
                                        ?>
                                        <a type="button" class="btn btn-default" onclick="upconvert({{{$bai_viet->id}}})"><i class='fa fa-level-up'></i></a>
                                    <a type="button" class="btn btn-default" onclick="downconvert({{{$bai_viet->id}}})"><i class='fa fa-level-down'></i></a> 
                                        <?php
                                    }
                                    ?> 
                                </td>
                                <td><?php
                                    $ten_chuyen_muc = DB::table('chuyen_muc_bai_viets')->where('id', $bai_viet->ChuyenMucBaiViet_Id)->pluck('ten_chuyen_muc');
                                    echo $ten_chuyen_muc;
                                    ?></td>
                                <td>{{ link_to_route('news.edit', '', array($bai_viet->id), array('class' => 'btn btn-info fa fa-edit')) }}</td>
                                <td>
                                    {{ Form::open(array('method' => 'DELETE', 'route' => array('news.destroy', $bai_viet->id),'class'=>'delete_bai_viet')) }}
                                    <button type="submit" class="btn btn-danger"><li class="fa fa-trash-o"></li></button>
                                    {{ Form::close() }}
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>

                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </div>
    </div>
</section>
<!-- DATA TABES SCRIPT -->
<script src="<?php echo asset('public/admin/js/plugins/datatables/jquery.dataTables.columnFilter.js'); ?>" type="text/javascript"></script>
<script type="text/javascript">
    // Setup - add a text input to each footer cell
        $('#dataTable tfoot th').each(function() {
            var title = $('#dataTable thead th').eq($(this).index()).text();
            $(this).html('<input type="text" placeholder="' + title + '" />');
        });

        var table = $('#dataTable').DataTable({
                    "scrollX": "100%",
                    "scrollCollapse": true,
                    "paging": true,
                    "bSort" : false,
                    "autoWidth": false,
                });
        // Apply the filter
        table.columns().eq(0).each(function(colIdx) {
            $('input', table.column(colIdx).footer()).on('keyup change', function() {
                table
                        .column(colIdx)
                        .search(this.value)
                        .draw();
            });
        });
</script>
<script type="text/javascript">
        function upconvert (id) {
        $.ajax({
            url: '<?php echo asset("ajax/upconvertpost")?>',
            type: 'post',
            dataType: 'html',
            data: {id: id},
        })
        .done(function(data) {
            console.log(data);
            window.location.reload();
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
        
    }
    function downconvert (id) {
         $.ajax({
            url: '<?php echo asset("ajax/downconvertpost")?>',
            type: 'post',
            dataType: 'html',
            data: {id: id},
        })
        .done(function(data) {
            console.log(data);
            window.location.reload();
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
 $('.delete_bai_viet').submit(function(event) {
       var txt;
        var r = confirm("Xác nhận xóa?");
        if (r == true) {
            return true;
        } else {
            return false;
        }
        return false;
    });


</script>
@stop